/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * editarCotizacion_Adicionales.java
 *
 * Created on 03/05/2011, 10:32:39
 */

package vista.cotizacion;

import controlador.cotizacion.GestorCotizacionAdicionales;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.SubObraXAdicional;
import modelo.SubObraXHerramienta;
import util.NTupla;
import util.TablaUtil;
import util.Tupla;

/**
 *
 * @author Emmanuel
 */
public class CotizacionAdicionales extends javax.swing.JPanel {

    private static final String TEXTO_BTN_AGREGAR = "Agregar";
    private static final String TEXTO_BTN_MODIFICAR = "Modificar";
    private static final String TEXTO_BTN_CANCELAR = "Cancelar";
    private static final String TEXTO_BTN_EDITAR = "Editar";    
    
    private GestorCotizacionAdicionales gestor;
    
    private SubObraXAdicional adicionalEditando;
    
    private boolean editando;
    
    /** Creates new form editarCotizacion_Adicionales */
    public CotizacionAdicionales(GestorCotizacionAdicionales gestor) {
        initComponents();
        this.gestor = gestor;
        this.gestor.setPantalla(this);
        this.gestor.initVentana();
        this.editando = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cmbTipoAdicional = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtConcepto = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtSubtotal = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        txtPrecioUnitario = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        btnAgregarAdicional = new javax.swing.JButton();
        btnQuitarAdicional = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbAdicionales = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        txtSubtotalAdicionales = new javax.swing.JTextField();
        btnEditar = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(440, 380));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo de Gasto (*)/Descripción"));

        cmbTipoAdicional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbTipoAdicionalActionPerformed(evt);
            }
        });

        txtConcepto.setColumns(20);
        txtConcepto.setLineWrap(true);
        txtConcepto.setRows(5);
        txtConcepto.setEnabled(false);
        jScrollPane1.setViewportView(txtConcepto);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(cmbTipoAdicional, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(cmbTipoAdicional, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Concepto"));

        jLabel1.setText("Cantidad (*)");

        jLabel3.setText("Precio Unitario (*)");
        jLabel3.setToolTipText("");

        jLabel5.setText("x");

        jLabel6.setText("=");

        txtSubtotal.setEditable(false);
        txtSubtotal.setBackground(new java.awt.Color(204, 204, 255));

        jLabel7.setText("Subtotal");

        txtCantidad.setEnabled(false);
        txtCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCantidadActionPerformed(evt);
            }
        });
        txtCantidad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCantidadFocusLost(evt);
            }
        });

        txtPrecioUnitario.setEnabled(false);
        txtPrecioUnitario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPrecioUnitarioActionPerformed(evt);
            }
        });
        txtPrecioUnitario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPrecioUnitarioFocusLost(evt);
            }
        });

        jLabel9.setText("$ ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                    .addComponent(txtCantidad))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPrecioUnitario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                    .addComponent(txtSubtotal))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(txtPrecioUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAgregarAdicional.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/down2.png"))); // NOI18N
        btnAgregarAdicional.setText("Agregar");
        btnAgregarAdicional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarAdicionalActionPerformed(evt);
            }
        });

        btnQuitarAdicional.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete.png"))); // NOI18N
        btnQuitarAdicional.setText("Quitar");
        btnQuitarAdicional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarAdicionalActionPerformed(evt);
            }
        });

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Gastos Varios"));

        tbAdicionales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo - Descripción", "Cantidad", "Precio Unitario", "SubTotal"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbAdicionales);

        jLabel8.setText("Subtotal Gestos Varios $");

        txtSubtotalAdicionales.setEditable(false);
        txtSubtotalAdicionales.setBackground(new java.awt.Color(204, 204, 255));
        txtSubtotalAdicionales.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSubtotalAdicionalesActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/Modify.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSubtotalAdicionales, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAgregarAdicional, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnQuitarAdicional)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnQuitarAdicional)
                    .addComponent(btnAgregarAdicional)
                    .addComponent(btnEditar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSubtotalAdicionales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarAdicionalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarAdicionalActionPerformed
        Tupla tipo = (Tupla)cmbTipoAdicional.getSelectedItem();
        String descripcion = txtConcepto.getText();
        
        if(tipo.getId()!=0)
        {   
            int cantidad = 0;
            try
            {
                cantidad = Integer.parseInt(txtCantidad.getText());
            } 
            catch(Exception e)
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","La cantidad ingresada no es válida");
                return;            
            }        

            if(cantidad<=0)
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","La cantidad ingresada debe ser positiva");
                return;
            }          

            double precio = 0;
            try
            {
                precio = Double.parseDouble(txtPrecioUnitario.getText().replaceAll(",","."));
            }
            catch(Exception e)
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","El precio ingresado no es válido");
                return;            
            }

            if(precio<=0)
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","El precio ingresado debe ser positivo");
                return;
            }  
            
            if(cantidad>0 )
            {
                if(precio>0)
                {
                    if(this.adicionalEditando==null)
                    {
                        AgregarAdicional(tipo,descripcion,cantidad,precio);
                    }
                    else
                    {
                        AgregarAdicional(this.adicionalEditando,tipo,descripcion,cantidad,precio);
                        
                    }
                    cargarModificacionAdicional(null);
                    editando=false;
                    
                }
                else
                {
                    MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","Debe ingresar un Precio positivo");
                }
            }
            else
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","Debe ingresar una Cantidad positiva");
            }
        }
        else
        {
            MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","Debe seleccionar un Tipo");
        }
}//GEN-LAST:event_btnAgregarAdicionalActionPerformed

    private void btnQuitarAdicionalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarAdicionalActionPerformed
        if(!editando)
        {
            if(tbAdicionales.getSelectedRow()!= -1)
            {
                DefaultTableModel dtm = (DefaultTableModel)tbAdicionales.getModel();
                NTupla ntp = (NTupla) dtm.getValueAt(tbAdicionales.getSelectedRow(), 0);
                gestor.quitarAlquilerCompra(ntp);

            }
            else
            {
                JOptionPane.showMessageDialog(this, "Debe seleccionar un Gasto General","Advertencia",JOptionPane.WARNING_MESSAGE);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Debe terminar la edición actual antes de realizar esta acción.","Advertencia",JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_btnQuitarAdicionalActionPerformed

    private void txtSubtotalAdicionalesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSubtotalAdicionalesActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtSubtotalAdicionalesActionPerformed

    private void cmbTipoAdicionalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbTipoAdicionalActionPerformed
        
        Tupla tipo = (Tupla)cmbTipoAdicional.getSelectedItem();
        if(tipo.getId()==0)
        {
            txtCantidad.setEnabled(false);
            txtPrecioUnitario.setEnabled(false);
            txtConcepto.setEnabled(false);
        }
        else
        {
            txtCantidad.setEnabled(true);
            txtPrecioUnitario.setEnabled(true);
            txtConcepto.setEnabled(true);            
        }
        
    }//GEN-LAST:event_cmbTipoAdicionalActionPerformed

    private void txtCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCantidadActionPerformed
        MostrarSubtotalConcepto();
    }//GEN-LAST:event_txtCantidadActionPerformed

    private void txtPrecioUnitarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPrecioUnitarioActionPerformed
        MostrarSubtotalConcepto();
    }//GEN-LAST:event_txtPrecioUnitarioActionPerformed

    private void txtCantidadFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCantidadFocusLost
        MostrarSubtotalConcepto();  
    }//GEN-LAST:event_txtCantidadFocusLost

    private void txtPrecioUnitarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPrecioUnitarioFocusLost
        MostrarSubtotalConcepto();
    }//GEN-LAST:event_txtPrecioUnitarioFocusLost

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        if(btnEditar.getText().equals(TEXTO_BTN_EDITAR))
        {
            if(tbAdicionales.getSelectedRow()!= -1)
            {
                DefaultTableModel dtm = (DefaultTableModel)tbAdicionales.getModel();
                NTupla ntp = (NTupla) dtm.getValueAt(tbAdicionales.getSelectedRow(), 0);

                SubObraXAdicional h = gestor.getAdicionalAgregada(ntp);
                cargarModificacionAdicional(h);
                
                editando = true;
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Debe seleccionar un Gasto General agregado para poder editarlo","Advertencia",JOptionPane.WARNING_MESSAGE);
            }
        }else if(btnEditar.getText().equals(TEXTO_BTN_CANCELAR))
        {
            // No modifico más
            cargarModificacionAdicional(null);
            editando=false;
        }
    }//GEN-LAST:event_btnEditarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarAdicional;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnQuitarAdicional;
    private javax.swing.JComboBox cmbTipoAdicional;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbAdicionales;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextArea txtConcepto;
    private javax.swing.JTextField txtPrecioUnitario;
    private javax.swing.JTextField txtSubtotal;
    private javax.swing.JTextField txtSubtotalAdicionales;
    // End of variables declaration//GEN-END:variables

    public void llenarComboTipos(ArrayList<Tupla> listaTipos) 
    {
        Tupla tp0 = new Tupla(0,"Seleccione un Tipo...");
        
        cmbTipoAdicional.removeAllItems();
        cmbTipoAdicional.addItem(tp0);
        for (int i = 0; i < listaTipos.size(); i++) 
        {
            Tupla tp = listaTipos.get(i);
            cmbTipoAdicional.addItem(tp);
        }
    }
    
    
    
    /**
     * Muestra un mensaje
     * @param tipo
     * @param titulo
     * @param mensaje 
     */
    public void MostrarMensaje(int tipo,String titulo,String mensaje)
    {
         JOptionPane.showMessageDialog(this.getParent(),mensaje,titulo,tipo);
    }

    public void llenarTabla(ArrayList<NTupla> listaFilas) {
                double total = 0;
        
        DefaultTableModel modelo = (DefaultTableModel)tbAdicionales.getModel();
        // Vacio la tabla por si ya tiene filas de otra llamada
        TablaUtil.vaciarDefaultTableModel(modelo);
        // lleno con los nuevos datos
        for (int i = 0; i < listaFilas.size(); i++) 
        {
            NTupla nt = listaFilas.get(i);
            
            String[] data = (String[])nt.getData();
            
            Object fila[] = new Object[5];
                fila[0] = nt;
                fila[1] = data[0];
                fila[2] = data[1];
                fila[3] = data[2];
                
                total += Double.valueOf(data[2]);
                
            modelo.addRow(fila);
        }
        
        // muestro el total calculado
        txtSubtotalAdicionales.setText(String.valueOf(total));
    }
    
    private void MostrarSubtotalConcepto()
    {
        double cantidad = 0;
        double precioUnitario = 0;
        
        try
        {
            cantidad = Double.parseDouble(txtCantidad.getText());
            precioUnitario = Double.parseDouble(txtPrecioUnitario.getText());        
        }
        catch(Exception e)
        {
            return;
        }
        
        txtSubtotal.setText("$"+cantidad*precioUnitario);
        
    }

    private void AgregarAdicional(Tupla tipo, String descripcion, int cantidad, double precio) 
    {
        gestor.AgregarAdicional(tipo,descripcion,cantidad,precio);
    }  
    
    private void cargarModificacionAdicional(SubObraXAdicional h) {
        this.adicionalEditando = h;
        if(this.adicionalEditando!=null)
        {
            btnAgregarAdicional.setText(TEXTO_BTN_MODIFICAR);
            btnEditar.setText(TEXTO_BTN_CANCELAR);
            btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete.png")));
 
            // Selecciona la herramienta
            for (int i = 0; i < cmbTipoAdicional.getItemCount(); i++) {
                Tupla tp = (Tupla)cmbTipoAdicional.getItemAt(i);
                if(tp.getId()==h.getTipoAdicional().getId()){
                    cmbTipoAdicional.setSelectedIndex(i);
                    continue;
                }
            }
            // Concepto
            txtConcepto.setText(h.getDescripcion());
            txtConcepto.setEnabled(true);
            txtConcepto.setEditable(true);
            
            // Costos
            txtCantidad.setText(String.valueOf(this.adicionalEditando.getCantidad()));
            txtCantidad.setEnabled(true);
            txtCantidad.setEditable(true);
            
            txtPrecioUnitario.setText(String.valueOf(this.adicionalEditando.getPrecioUnitario()));
            txtPrecioUnitario.setEnabled(true);
            txtPrecioUnitario.setEditable(true);
            
            MostrarSubtotalConcepto();
        }
        else{
            btnAgregarAdicional.setText(TEXTO_BTN_AGREGAR);
            btnEditar.setText(TEXTO_BTN_EDITAR);
            btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/Modify.png")));
            txtConcepto.setText("");
            txtCantidad.setText("");
            txtPrecioUnitario.setText("");
            txtSubtotal.setText("");
            cmbTipoAdicional.setSelectedIndex(0);
        }
    }

    private void AgregarAdicional(SubObraXAdicional editando, Tupla tipo, String descripcion, int cantidad, double precio) {
        gestor.AgregarAdicional(editando,tipo,descripcion,cantidad,precio);
    }
    
    
}
